package com.example.mynote.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mynote.R;
import com.example.mynote.base.BaseActivity;
import com.example.mynote.entity.Note;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class AddActivity extends BaseActivity {

    private android.widget.ImageButton addCancel;
    private android.widget.TextView addDate;
    private android.widget.TextView addTime;
    private android.widget.ImageButton addSave;
    private android.widget.EditText addTitle;
    private android.widget.EditText addContent;

    @Override
    protected int initLayout() {
        return R.layout.activity_add;
    }

    @Override
    protected void initView() {

        addCancel = (ImageButton) findViewById(R.id.add_cancel);
        addDate = (TextView) findViewById(R.id.add_date);
        addTime = (TextView) findViewById(R.id.add_time);
        addSave = (ImageButton) findViewById(R.id.add_save);
        addTitle = (EditText) findViewById(R.id.add_title);
        addContent = (EditText) findViewById(R.id.add_content);
    }

    @Override
    protected void initData() {
        addCancel.setOnClickListener(v -> onBackPressed());
        SimpleDateFormat time = new SimpleDateFormat("HH:mm", Locale.CHINA);
        SimpleDateFormat month = new SimpleDateFormat("MM月/E", Locale.CHINA);
        addDate.setText(month.format(new Date()));
        addTime.setText(time.format(new Date()));

        addSave.setOnClickListener(v -> {
            String title = addTitle.getText().toString();
            String content = addContent.getText().toString();
            TranslateAnimation animation = new TranslateAnimation(0f, 5f, 0f, 5f);
            CycleInterpolator interpolator = new CycleInterpolator(3f);
            animation.setInterpolator(interpolator);
            animation.setDuration(500);
            if (!title.equals("")) {
                if (!content.equals("")) {
                    new Note(title, content, new Date().getTime()).save();
                    finish();
                } else {
                    addContent.startAnimation(animation);
                    Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
                }
            } else {
                addTitle.startAnimation(animation);
                Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
            }
        });

    }
}